package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;

import org.junit.Test;

import com.meterware.httpunit.UploadFileSpec;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Tests the XmlActionBean class.
 * 
 * @author Shaoxuan Zhang
 *
 */
public class TestXmlActionBean extends TestWeb {
  private static final String FORM_ID = "Xml";
  private static final String IMPORT_BUTTON = "importXml";
  private static final String FILE_FIELD = "importXmlFile";
  private static final String PATH = "xml/examples/";
  private static final String COLLABORATIONS_TEST_XML = "collaborations.test.xml";
  private static final String ORGANIZATIONS_TEST_XML = "organizations.test.xml";
  private static final String RESEARCHERS_TEST_XML = "researchers.test.xml";
  private static final String COLLABORATIONS_TITLE = "ISERN - Collaborations";
  private static final String ORGANIZATIONS_TITLE = "ISERN - Organizations";
  private static final String RESEARCHERS_TITLE = "ISERN - Researchers";


  /**
   * Tests that the form requires a name.
   *    
   * @throws Exception If problems occur
   */
  @Test
  public void testRequiresName() throws Exception {
    assertNotNull("the user session should not be null", 
        conversation.getCookieValue("JSESSIONID"));
    //Import Collaborations
    WebResponse response = conversation.getResponse(getAppUrl() + "/" + "xml.jsp");
    WebForm form = response.getFormWithID(FORM_ID); 
    WebRequest request = form.getRequest(IMPORT_BUTTON);
    UploadFileSpec[] uploadCollaborationsFile = 
      {new UploadFileSpec(new File(PATH + COLLABORATIONS_TEST_XML))};
    request.setParameter(FILE_FIELD, uploadCollaborationsFile);
    response = conversation.getResponse(request);
    assertEquals("Collaborations page should be displayed.", COLLABORATIONS_TITLE, 
        response.getTitle());    

    //Import Organizations
    response = conversation.getResponse(getAppUrl() + "/" + "xml.jsp");
    form = response.getFormWithID(FORM_ID); 
    request = form.getRequest(IMPORT_BUTTON);
    UploadFileSpec[] uploadOrgnizationsFile = 
      {new UploadFileSpec(new File(PATH + ORGANIZATIONS_TEST_XML))};
    request.setParameter(FILE_FIELD, uploadOrgnizationsFile);
    response = conversation.getResponse(request);
    assertEquals("Organizations page should be displayed.", ORGANIZATIONS_TITLE, 
        response.getTitle());    

    //Import Researchers
    response = conversation.getResponse(getAppUrl() + "/" + "xml.jsp");
    form = response.getFormWithID(FORM_ID); 
    request = form.getRequest(IMPORT_BUTTON);
    UploadFileSpec[] uploadResearchersFile = 
      {new UploadFileSpec(new File(PATH + RESEARCHERS_TEST_XML))};
    request.setParameter(FILE_FIELD, uploadResearchersFile);
    response = conversation.getResponse(request);
    assertEquals("Researchers page should be displayed.", RESEARCHERS_TITLE, 
        response.getTitle());    
    
  }
}
